package com.yijiankang.threaddemo.example.atomic;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author Mr
 * @title: ConcurrentTest
 * @projectName mytest
 * @description: TODO
 * @date 2019/7/1611:17
 */
@Slf4j
public class ConcurrentReferenceFileUpdaterTest {

    //实例化  第二个字段
    //AtomicIntegerFieldUpdater更新指定类的某一个字段的值  字段值必须时 volatile修饰  并且是非static
   public static AtomicIntegerFieldUpdater<ConcurrentReferenceFileUpdaterTest> updater = AtomicIntegerFieldUpdater.newUpdater(ConcurrentReferenceFileUpdaterTest.class,"count");
   @Getter
   public volatile  int count = 100;

   private static ConcurrentReferenceFileUpdaterTest compareAndSet = new ConcurrentReferenceFileUpdaterTest();

    public static void main(String[] args) {
        //是100时，更新成120
        if(updater.compareAndSet(compareAndSet,100,120)){
            log.info("update success:{}",compareAndSet.getCount());
        }
        if(updater.compareAndSet(compareAndSet,100,120)){
            log.info("update success:{}",compareAndSet.getCount());
        }else{
            log.info("update error:{}",compareAndSet.getCount());
        }

    }




}
